/**
 * Filename: LocalViewHolder.java
 *
 * @author Firefly
 * @contact postfirefly@163.com
 * @time 2016/3/17 15:12
 */
package com.github.postsanf.yinian.utils;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.net.Uri;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.github.postsanf.yinian.R;
import com.github.postsanf.yinian.constants.CommonConstants;
import com.nostra13.universalimageloader.core.ImageLoader;

public class LocalViewHolder {
    private final SparseArray<View> mViews;
    private int mPosition;
    private View mConvertView;
    private ImageLoader imageLoader;
    private String localStr = "file://";
    private Context mContext;
    private Resources mResource;

    public void setResource(Resources mResource) {
        this.mResource = mResource;
    }

    private LocalViewHolder(Context mContext, ViewGroup parent, int layoutId,
                            int position) {
        this.mPosition = position;
        this.mViews = new SparseArray<View>();
        this.mContext = mContext;
        mConvertView = LayoutInflater.from(mContext).inflate(layoutId, parent,
                false);
        // setTag
        imageLoader = ImageLoader.getInstance();
        mConvertView.setTag(this);
    }

    // 拿到一个ViewHolder对象
    public static LocalViewHolder get(Context context, View convertView,
                                      ViewGroup parent, int layoutId, int position) {
        LocalViewHolder holder = null;
        if (convertView == null) {
            holder = new LocalViewHolder(context, parent, layoutId, position);
        } else {
            holder = (LocalViewHolder) convertView.getTag();
            holder.mPosition = position;
        }
        return holder;
    }

    public View getConvertView() {
        return mConvertView;
    }

    // 通过控件的Id获取对应的控件，如果没有则加入views
    public <T extends View> T getView(int viewId) {
        View view = mViews.get(viewId);
        if (view == null) {
            view = mConvertView.findViewById(viewId);
            mViews.put(viewId, view);
        }
        return (T) view;
    }

    // 为TextView设置字符串
    public LocalViewHolder setText(int viewId, String text) {
        TextView view = getView(viewId);
        view.setText(text);
        return this;
    }

    // 为ImageView设置图片
    public LocalViewHolder setImageResource(int viewId, int drawableId) {
        ImageView view = getView(viewId);
        if (mResource != null && drawableId == R.drawable.picture_unselected) {
            view.setImageDrawable(mResource.getDrawable(mResource.getIdentifier("picture_unselected", CommonConstants.THEME_DRAWABLE, CommonConstants.THEME_PACKAGE)));
        } else {
            view.setImageResource(drawableId);
        }
        return this;
    }

    // 为ImageView设置图片
    public LocalViewHolder setImageBitmap(int viewId, Bitmap bm) {
        ImageView view = getView(viewId);
        view.setImageBitmap(bm);
        return this;
    }

    // 为ImageView设置图片
    public LocalViewHolder setImageByUrl(int viewId, String url) {
        if (url.startsWith("http")) {
            ImageOptHelper.showFrescoNative(Uri.parse(url), (SimpleDraweeView) getView(viewId), mContext);
        } else {
            ImageOptHelper.showFrescoNative(Uri.parse(localStr + url), (SimpleDraweeView) getView(viewId), mContext);
        }
        return this;
    }


//    public LocalViewHolder setVideoThumbByUrl(int viewId, String url) {
//        ImageView spview = (ImageView) getView(viewId);
//        spview.setImageBitmap(UIHelper.getVideoThumbnail(url));
//        return this;
//    }

    public LocalViewHolder setImageByUrl2(int viewId, String url) {
        Uri uri = Uri.parse(localStr + url);
        ImageOptHelper.showFrescoNative(uri, (SimpleDraweeView) getView(viewId), mContext);
        return this;
    }

    public int getPosition() {
        return mPosition;
    }

}

